import vueThis from "../main.js";
import { Message, Notification } from "element-ui"
import { getArea } from "@/API/public"
// 时间格式化
const secondDate = (data, isMillis, type) => {
    let time = new Date(data * 1000);
    time = isMillis ? new Date(data) : new Date(data * 1000);
    let year = time.getFullYear();
    let month = time.getMonth() + 1;
    month = month < 10 ? '0' + month : month
    let day = time.getDate();
    day = day < 10 ? '0' + day : day
    let hours = time.getHours();
    hours = hours < 10 ? '0' + hours : hours
    let minute = time.getMinutes();
    minute = minute < 10 ? '0' + minute : minute
    let second = time.getSeconds();
    second = second < 10 ? '0' + second : second;
    if (type) {
        return year + '-' + month + '-' + day
    } else {
        return year + '-' + month + '-' + day + ' ' + hours + ':' + minute + ':' + second
    }
}
// 毫秒转秒
const millisecond = (second) => {
    return Math.floor(second / 1000)
}
// 消息提示
const message = (message, type, duration) => {
    Message({
        message: message || "",
        type: type || "warning",
        duration: duration || 2000
    });
}
// 通知
const toast = (title, type, message) => {
    Notification({
        title: title || '成功',
        type: type || 'success',
        message: message || '',
    })
}
// 获取省市区
const getAreaList = async () => {
    let res = await getArea();
    let areaList=[]
    res.data.data.provinceData.forEach(item => {
        vueThis.$set(item, "children", [])
        res.data.data.cityData.forEach(value => {
            value.forEach(ite => {
                if (item.id == ite.pid) {
                    item.children.push(ite)
                }
            })
        })
    })
    areaList = res.data.data.provinceData
    return {
        areaList
    }
}
export default {
    install: (Vue, options) => {
        Vue.prototype.secondDate = secondDate
        Vue.prototype.millisecond = millisecond
        Vue.prototype.message = message;
        Vue.prototype.toast = toast;
        Vue.prototype.getAreaList = getAreaList
    }
}